<template>
  <div class="app-container">
    <div class="custom_card_0">
      <MenuTabs :list="tabs_list1" :column="10" :labelStyle="{ width: '100px' }" :active="countStatusIndex"
        @tabs_click="changeCountStatus"></MenuTabs>
    </div>
    <div class="custom_card_0" v-show="showSearch">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" size="small">
        <el-form-item label="车牌筛选" prop="carNo">
          <el-input v-model="queryParams.carNo" placeholder="车牌筛选" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="处理状态" prop="processingStatus">
          <el-select v-model="queryParams.processingStatus" placeholder="请选择处理状态" clearable>
            <el-option label="未处理" value="1" />
            <el-option label="已处理未缴费" value="2" />
            <el-option label="已处理" value="3" />
            <el-option label="已缴费未处理" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="custom_card">
      <el-row :gutter="10" class="mb8 card_title" type="flex">
        <el-col :span="1.5">
          <el-button class="first-plain-btn" size="medium" @click="handleAdd"
            v-hasPermi="['car:illegal:add']">新&nbsp;&nbsp;增</el-button>
        </el-col>

        <el-col :span="1.5">
          <el-button  class="sec-plain-btn" size="medium"
            @click="handleQueryIllegal" v-hasPermi="['car:illegal:query']">违章查询</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button  class="thr-plain-btn" size="medium" @click="batchQuery" v-hasPermi="['car:illegal:workorder']">违章工单</el-button>

        </el-col>

        <el-col :span="1.5">
          <el-button  class="four-plain-btn" size="medium"
                     @click="setaccount" v-hasPermi="['car:illegal:account']">账号配置</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button  class="five-plain-btn" size="medium" :loading="exportLoading"
                     @click="handleExport" v-hasPermi="['car:illegal:export']">导出</el-button>
        </el-col>

        <div class="card_title_info">
          <div v-if="countInfo.inQuery">
            违章结果查询中...
          </div>

          <div v-else>
            最新查询时间:{{ countInfo.lastTime }} 查询结果:{{ countInfo.lastTotal }}条
          </div>
          <!-- <div style="color:red">
            余额不足！定时违章查询功能无法正常使用
          </div> -->
        </div>



        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="illegalList" @selection-change="handleSelectionChange" border>
        <el-table-column label="品牌/车系/车型" header-align="center" prop="carInfo" min-width="240"
          :show-overflow-tooltip="true" />
        <el-table-column label="车牌号" align="center" prop="carNo" min-width="120" :show-overflow-tooltip="true" />
        <el-table-column label="车架号" align="center" prop="vinNo" min-width="120" :show-overflow-tooltip="true" />
        <el-table-column label="违章单据号" align="center" prop="documentNo" min-width="120" :show-overflow-tooltip="true" />
        <el-table-column label="关联客户" align="center" prop="orderNo" min-width="220" :show-overflow-tooltip="true">
          <template slot-scope="{ row }">
            <div style="text-align: start" @click="handleMemberDetail(row.memberNo)" v-if="row.memberNo">
              客户:<span style="font-size: 14px; color: #1890ff;cursor:pointer;">{{ row.memberName }}</span>
            </div>
            <div style="text-align: start" @click="handleOrderDetail(row.orderNo)" v-if="row.orderNo">
              订单:<span style="font-size: 14px; color: #1890ff;cursor:pointer;">{{ row.orderNo }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="违章时间" align="center" prop="illegalTime" min-width="160" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.illegalTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="违章地点" align="center" prop="illegalLocation" min-width="100"
          :show-overflow-tooltip="true" />

        <!--            <el-table-column label="违章内容" align="center" prop="illegalDetail" />-->
        <el-table-column label="违章扣分" align="center" prop="pointsDeduction" min-width="100"
          :show-overflow-tooltip="true" />
        <el-table-column label="罚款金额" align="center" prop="illegalFines">
          <template slot-scope="{ row }">
            ￥{{ row.illegalFines }}
          </template>
        </el-table-column>
        <el-table-column label="处理状态" align="center" prop="processingStatus" min-width="120"
          :show-overflow-tooltip="true">
          <template slot-scope="{ row }">
            {{
              { 1: '未处理', 2: '已处理未缴费', 3: '已处理' , 4: '已缴费未处理' }[
              Number(row.processingStatus)
              ]
            }}
          </template>
        </el-table-column>
        <el-table-column label="所属门店" align="center" prop="deptName" min-width="100" :show-overflow-tooltip="true" />
        <!--            <el-table-column label="处理机构" align="center" prop="processingAgency" />-->
        <!--            <el-table-column label="所属租户" align="center" prop="tenantName" />-->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="left" width="160">
          <!--              :disabled="!(scope.row.processingStatus < 3)"-->
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="handleUpdate(scope.row)"
              v-hasPermi="['car:illegal:edit']">修改</el-button>
            <el-button size="small" type="text" @click="handleInfo(scope.row)">详情</el-button>
            <!--              :disabled="!(scope.row.processingStatus < 3)"-->
            <el-button size="small" type="text" @click="handleDelete(scope.row)"
              v-hasPermi="['car:illegal:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <el-dialog title="违章查询" width="1000px" append-to-body destroy-on-close :visible.sync="showQueryDialog">
      <QueryIllegal v-if="showQueryDialog" @success="submitIllegalQuery" @cancel="showQueryDialog = false"></QueryIllegal>
    </el-dialog>

    <!-- 新增违章工单 -->
    <add-illegal-order ref="addIllegalOrder" @success="submitIllegalQuery"></add-illegal-order>

  </div>
</template>

<script>
import {
  listIllegal,
  getIllegal,
  delIllegal,
  addIllegal,
  updateIllegal,
  exportIllegal,
  illegalCount,
} from '@/api/car/illegal'
import MmvAdd from './add.vue'
import MmvInfo from './info.vue'
import MenuTabs from '@/components/custom/newMenuTabs.vue'
import QueryIllegal from '@/views/car/illegal/queryillegal.vue'
import * as api from "@/api/car/illegal";
import AddIllegalOrder from './components/addIlleaglOrder.vue';
export default {
  name: 'Illegal',
  components: {
    MmvAdd,
    MmvInfo,
    MenuTabs,
    QueryIllegal,
    AddIllegalOrder
  },
  data() {
    return {
      tabs_list1: [
        {
          key: 0,
          name: '全部违章'
        },
        {
          key: 15,
          name: '超15天未处理'
        },
        {
          key: 30,
          name: '超30天未处理'
        },
      ],
      //违章查询弹窗
      showQueryDialog: false,
      nowId: null,
      loadingBtn: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      countStatusIndex: 0,
      // 违章表格数据
      illegalList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        unprocessedDays: 0,
        carId: null,
        carNo: null,
        vinNo: null,
        carInfo: null,
        documentNo: null,
        illegalTime: null,
        illegalLocation: null,
        illegalType: null,
        illegalDetail: null,
        processingAgency: null,
        pointsDeduction: null,
        illegalFines: null,
        processingStatus: null,
        payPersonType: null,
        paymentStatus: null,
        illegalPoundage: null,
        totalMoney: null,
        repayment: null,
        handlers: null,
        isAvailable: null,
        deptId: null,
        tenantId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //统计信息
      countInfo: {
        total: '',
        fifteen: '',
        thirty: '',
        inQuery: false,
        lastTime: '',
        lastTotal: '',
      }
    }
  },
  activated() {
    this.getList()
  },
  created() {
    let processingStatus = this.$route.query.processingStatus
    if(processingStatus){
      this.queryParams.processingStatus = processingStatus
    }

    this.getList()
  },
  methods: {
    /** 查询违章列表 */
    getList() {
      this.loading = true
      listIllegal(this.queryParams).then((response) => {
        this.illegalList = response.rows
        this.total = response.total
        this.loading = false
      })

      this.getIllegalCount()
    },
    //违章数量
    getIllegalCount() {
      illegalCount().then(res => {
        console.log("Illegal", res);
        this.countInfo = res.data

        this.tabs_list1[0].name = `全部违章  ${res.data.total}`
        this.tabs_list1[1].name = `超15天未处理  ${res.data.fifteen}`
        this.tabs_list1[2].name = `超30天未处理  ${res.data.thirty}`

      })
    },
    //切换状态
    changeCountStatus(status) {
      this.countStatusIndex = status
      this.queryParams.unprocessedDays = status
      this.queryParams.pageNum = 1
      this.getList()
    },

    //违章查询弹窗点确定了
    submitIllegalQuery(data) {
      this.showQueryDialog = false



      this.queryParams.pageNum = 1
      this.getList()
    },

    //废弃
    batchQuery() {
      this.$router.push({ path: '/illegal/batchqueryIllegal/' })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        carId: null,
        carNo: null,
        vinNo: null,
        carInfo: null,
        documentNo: null,
        illegalTime: null,
        illegalLocation: null,
        illegalType: null,
        illegalDetail: null,
        processingAgency: null,
        pointsDeduction: null,
        illegalFines: null,
        processingStatus: 0,
        payPersonType: null,
        paymentStatus: 0,
        illegalPoundage: null,
        totalMoney: null,
        repayment: null,
        handlers: null,
        delFlag: null,
        isAvailable: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deptId: null,
        tenantId: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 订单详情按钮 */
    handleOrderDetail(orderNo) {
      this.$router.push({
        path: '/orderBasic/detail',
        query: { orderNo: orderNo }
      })
    },
    /** 客户详情按钮 */
    handleMemberDetail(memberNo) {
      this.$router.push({
        name: 'member_detail',
        query: { memberNo: memberNo },
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ name: `illegal_add` })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({ name: `illegal_edit`, query: { id: row.id } })
    },
    /** 查看按钮操作 */
    handleInfo(row) {
      this.$router.push({ name: 'illegal_detail', query: { id: row.id } })
    },

    // 违章查询
    handleQueryIllegal() {
      // 获取当前租户配置的违章查询账号
      api.illegalAcountContainType().then(res => {
        let type = res.data;
        if (type == -1) {//未配置
          this.$message('没有可用的违章查询账号,暂时无法查询');
        } else {//已配置
          this.$refs.addIllegalOrder.show(type);
        }
      })
    },

    setaccount(){
      this.$router.push({ path: '/risk/illegal/account'})
    },

    sendMsg(){

    },

    setMsg(){

    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除数据项？')
        .then(function () {
          return delIllegal(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal
        .confirm('是否确认导出所有违章数据项？')
        .then(() => {
          this.exportLoading = true
          return exportIllegal(queryParams)
        })
        .then((response) => {
          this.$download.name(response.msg)
          this.exportLoading = false
        })
        .catch(() => { })
    },
  },
}
</script>
<style scoped>
.card_title {
  align-items: center;
}

.card_title_info {
  flex: 1;
  align-self: flex-end;
  font-size: 13px;
  color: #333;
  margin-bottom: 5px;
}
</style>
